home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 05 - User Interaction / StylDialog / StylDialog.c next >
Encoding:
C/C++ Source or Header  |  1995-03-06  |  4.4 KB  |  178 lines  |  [TEXT/MMCC]

  1. /* StylDialog demo by Ingemar Ragnemalm 1995 */
  2. /* Based on a help system by Cary Torkelson */
  3.  
  4.  
  5. #include <QDOffScreen.h>
  6.  
  7. void ShowStyledTextDialog(short textResourceID);
  8.  
  9.  
  10.     short theType;
  11.     short itemHit;
  12.     Handle itemhdl;
  13.     Rect itembox;
  14.     DialogPtr theDialog;
  15.     TEHandle styledTextTEHandle;
  16.     Rect textRect;
  17.     ControlHandle theScrollbar;
  18.     short numberLinesPerPage;
  19.  
  20.  
  21. static void ScrollTextToScrollValue()
  22. {
  23.     short oldScroll;
  24.     short newScroll;
  25.     short scrollbarValue;
  26.  
  27.     HLock((Handle)styledTextTEHandle);
  28.  
  29.     oldScroll = (**styledTextTEHandle).viewRect.top - (**styledTextTEHandle).destRect.top;
  30.     scrollbarValue = GetCtlValue(theScrollbar);
  31.     if ( scrollbarValue == 0 )
  32.         newScroll = 0;
  33.     else
  34.         newScroll = TEGetHeight(GetCtlValue(theScrollbar), 0, styledTextTEHandle);
  35.     TEScroll(0, oldScroll - newScroll, styledTextTEHandle);
  36.  
  37.     HUnlock((Handle)styledTextTEHandle);
  38. } /*ScrollTextToScrollValue*/
  39.  
  40.  
  41. static pascal void ScrollText(ControlHandle theControl, short thePart)
  42. {
  43.     short delta;
  44.     short oldValue;
  45.  
  46.     switch ( thePart )
  47.     {
  48.         case inUpButton: 
  49.             delta = -1;break;
  50.         case inDownButton: 
  51.             delta = 1;break;
  52.         case inPageUp: 
  53.             delta = -numberLinesPerPage;break;
  54.         case inPageDown: 
  55.             delta = numberLinesPerPage;break;
  56.         default: ;
  57.     }
  58.     if ( thePart != 0 )
  59.     {
  60.         oldValue = GetCtlValue(theControl);
  61.         SetCtlValue(theControl, oldValue + delta);
  62.         ScrollTextToScrollValue();
  63.     }
  64. } /*ScrollText*/
  65.  
  66.  
  67. static pascal Boolean MyDialogFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  68. {
  69.     Point clickPoint;
  70.     Boolean returnValue;
  71.     short thePart;
  72.     ControlHandle theControl;
  73.     short chcode;
  74.  
  75.     returnValue = false;
  76.     if ( theEvent->what == mouseDown )
  77.     {
  78.         clickPoint = theEvent->where;
  79.         GlobalToLocal(&clickPoint);
  80.         thePart = FindControl(clickPoint, theDialog, &theControl);
  81.         if ( theControl == theScrollbar )
  82.             if ( thePart == inThumb )
  83.                 {
  84.                     thePart = TrackControl(theScrollbar, clickPoint, nil);
  85.                     ScrollTextToScrollValue();
  86.                 }
  87.             else
  88.                 thePart = TrackControl(theScrollbar, clickPoint, &ScrollText);
  89.     }
  90.     else if ( theEvent->what == keyDown )
  91.     {
  92.         chcode = (theEvent->message & charCodeMask);
  93.         if ( (chcode == 13) || (chcode == 3) )
  94.         {
  95.             *itemHit = ok;
  96.             returnValue = true;
  97.         }
  98.     }
  99.     return returnValue;
  100. } /*MyDialogFilter*/
  101.  
  102.  
  103. void ShowStyledTextDialog(short textResourceID)
  104. {
  105. #define    kHelpID    128
  106. #define    kHelpTextTEID 2
  107. #define    kScrollBarID 4
  108.  
  109.     Rect tempRect;
  110.     Handle textResourceHandle;
  111.     StScrpHandle stylResourceHandle;
  112.     short maxScrollValue;
  113.  
  114. /*Get the dialog*/
  115.     theDialog = GetNewDialog(kHelpID, 0L, (WindowPtr)-1);
  116.     SetPort(theDialog);
  117. /*Get the user item defining where the text goes.*/
  118.     GetDItem(theDialog, kHelpTextTEID, &theType, &itemhdl, &textRect);
  119. /*Get the scrollbar handle*/
  120.     GetDialogItem(theDialog, kScrollBarID, &theType, (Handle *) &theScrollbar, &tempRect);
  121. /*Make a TextEdit record*/
  122.     styledTextTEHandle = TEStylNew(&textRect, &textRect);
  123. /*Get the 'TEXT'/'styl' resources and insert them into the TextEdit record.*/
  124.     textResourceHandle = GetResource('TEXT', textResourceID);
  125.     stylResourceHandle = (StScrpHandle)GetResource('styl', textResourceID);
  126.     HLock(textResourceHandle);
  127.     TEStylInsert(*textResourceHandle, SizeResource(textResourceHandle), stylResourceHandle, styledTextTEHandle);
  128.     HUnlock(textResourceHandle);
  129.     ReleaseResource(textResourceHandle);
  130.     ReleaseResource((Handle)stylResourceHandle);
  131. /*Set scrollbar values depending on the text*/
  132.     numberLinesPerPage = (textRect.bottom - textRect.top) / (TEGetHeight((**styledTextTEHandle).nLines, 0, styledTextTEHandle) / (**styledTextTEHandle).nLines);
  133.     maxScrollValue = (**styledTextTEHandle).nLines - numberLinesPerPage;
  134.     if ( (maxScrollValue <= 0) )
  135.         HiliteControl(theScrollbar, 255);
  136.     else
  137.         SetCtlMax(theScrollbar, maxScrollValue);
  138. /*Frame the text area and redraw it*/
  139.     tempRect = textRect;
  140.     InsetRect(&tempRect, -1, -1);
  141.     FrameRect(&tempRect);
  142.     TEUpdate(&textRect, styledTextTEHandle);
  143.  
  144.     InitCursor ();
  145. /*Run ModalDialog until the user clicks OK*/
  146.     itemHit = 0;
  147.     while (itemHit != ok)
  148.         ModalDialog(&MyDialogFilter, &itemHit);
  149.  
  150.     DisposeControl(theScrollbar);
  151.     DisposeDialog(theDialog);
  152.     TEDispose(styledTextTEHandle);
  153. } /*ShowStyledTextDialog*/
  154.  
  155.  
  156. /* Standard inits */
  157.  
  158. static void InitToolbox(void) {
  159.     InitGraf (&qd.thePort);
  160.     InitFonts ();
  161.     FlushEvents (everyEvent,0);
  162.     InitWindows ();
  163.     InitMenus ();
  164.     TEInit ();
  165.     InitDialogs (nil);
  166.     InitCursor ();
  167. } /*InitToolbox*/
  168.  
  169.  
  170. /* Main program */
  171.  
  172. void main(void)
  173. {
  174.     InitToolbox();
  175.  
  176.     ShowStyledTextDialog(128);
  177. } /*main*/
  178.